package com.acme.bankapp.domain.account;

import com.acme.bankapp.domain.exceptions.NotEnoughFundsException;


public class SavingAccount extends AbstractAccount {

	public SavingAccount(int id, double amount) {
		super(id, amount);
	}
	
	@Override
	public void deposit(double amount) {
		validateAmount(amount);
		balance += amount;
	}

	@Override
	public void withdraw(double amount) throws NotEnoughFundsException {
		validateAmount(amount);
		if (balance > amount) {
			balance -= amount;
		} else {
			throw new NotEnoughFundsException();
		}
	}

	@Override
	public double maximumAmountToWithdraw() {
		return balance;
	}
	
	@Override
	public int hashCode() {
		final int prime = 31;
		return getId() + (int) getBalance() * prime;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		SavingAccount other = (SavingAccount) obj;
		if (getId() != other.getId())
			return false;
		if (getBalance() != other.getBalance())
			return false;
		return true;
	}
	
	@Override
	public String toString() {
		StringBuilder builder = new StringBuilder("Saving account. Id = ");
		builder.append(id).append(", balance = ").append(getBalance());
		return builder.toString();
	}
}
